package com.nowcoder.community.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.print.DocFlavor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@Controller
public class AlphaController {

    @RequestMapping("/http")
    public void http(HttpServletRequest request, HttpServletResponse response) {
        // 获取请求数据
        System.out.println(request.getMethod());
        System.out.println(request.getServletPath());
        Enumeration<String> enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement();
            String value = request.getHeader(name);
            System.out.println(name + ": " + value);
        }
        System.out.println(request.getParameter("code"));
        //返回响应数据
        response.setContentType("text/html;charset=utf-8");
        try (PrintWriter writer = response.getWriter()) {
            writer.write("<h1>牛客网</h1>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //GET 请求 :浏览器向服务请求数据，传输的数据量有限
    // /students?current=1?limit=20
    @RequestMapping(path = "/students", method = RequestMethod.GET)
    @ResponseBody
    public String getStudents(
        @RequestParam(name = "current", required = false, defaultValue = "1") int current,
        @RequestParam(name = "limit", required = false, defaultValue = "10") int limit
    ) {

        System.out.println(current);
        System.out.println(limit);
        return "some students";
    }

    //查询一个学生的信息 请求：/students/123
    @RequestMapping(path = "/student/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String getStudent(@PathVariable("id") int id) {
        System.out.println(id);
        return "a student";
    }

    //POST请求 ：浏览器向服务器提交数据,接受表单信息
    //静态网页的访问方式：http://localhost:8080/community/html/student.html
    @RequestMapping(path = "/student", method = RequestMethod.POST)
    @ResponseBody
    public String saveStudent(String name, int age) {
        System.out.println(name);
        System.out.println(age);
        return "success";
    }

    //响应HTML数据
    @RequestMapping(path = "/teacher", method = RequestMethod.GET)
    public ModelAndView getTeacher() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name", "张三");
        modelAndView.addObject("age", 30);
        modelAndView.setViewName("/demo/view");
        return modelAndView;
    }

    /**返回字符串的错误：
     * 原因：在controller层使用的是@RestController注解
     * 修改方案：把@RestController改成@Controller
     * 分析：RestController = Controller + ResponseBody.使用@RestController,
     * 返回的内容是你return中的内容，如果是return “Hello World”，页面显示的就是Hello World。
     * 使用@Controller，返回的是return中对应的页面,比如return “hello”,页面的名称是hello。
     * @param model
     * @return
     */
    @RequestMapping(path = "/school", method = RequestMethod.GET)
    public String getSchool(Model model) {
        model.addAttribute("name","北京大学");
        model.addAttribute("age", 88);
        return "/demo/view";
    }

    //响应JSON数据（异步请求）
    //Java对象-->JSON字符串-->JS对象
    @RequestMapping(path = "/emp", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEmp() {
        Map<String, Object> emp = new HashMap<>();
        emp.put("name", "张三");
        emp.put("age", 22);
        emp.put("salary", 8000);
        return emp;//自动转换成JSON格式,(自定义类型也可以)
    }

    @RequestMapping(path = "/emps", method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String, Object>> getEmps() {
        List<Map<String, Object>>emps = new ArrayList<>();
        Map<String, Object> emp1 = new HashMap<>();
        emp1.put("name", "张三");
        emp1.put("age", 22);
        emp1.put("salary", 8000);
        emps.add(emp1);
        Map<String, Object> emp2 = new HashMap<>();
        emp2.put("name", "李四");
        emp2.put("age", 21);
        emp2.put("salary", 8200);
        emps.add(emp2);
        Map<String, Object> emp3 = new HashMap<>();
        emp3.put("name", "王五");
        emp3.put("age", 22);
        emp3.put("salary", 8888);
        emps.add(emp3);
        return emps;
    }

}
